package core.proto;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JLabel;

import app.xml.model.AnnotationTag;
import core.util.logging.SimpleLogger;
import core.util.logging.constants.Location;

public class PaintDoc extends JEditorPane
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private static final SimpleLogger LOGGER = SimpleLogger.getLogger();

    private int x;

    private int y;

    private String selectedText;

    private JLabel picLabel;

    public void set( int x, int y, String selectedText ) {
        this.x = x;
        this.y = y;
        this.selectedText = selectedText;
    }

    public void paintComponents( Graphics g ) {
        super.paintComponents( g );
        System.out.println( "Painting components" );
        ImageIcon imgIcon = new ImageIcon( AllComponents.imagePath );

        picLabel = new JLabel( imgIcon );
        picLabel.setName( selectedText );
        picLabel.setVisible( true );
        picLabel.setEnabled( true );
        picLabel.setBounds( x, y, 40, 50 );
        add( picLabel );
        //document.setVisible( true );
        setVisible( true );
        // document.updateUI();
        picLabel.addMouseListener( new MouseAdapter()
        {
            public void mouseClicked( MouseEvent me ) {
                System.out.println( "Annotation tab clicked..." );
                // see if key is present in the annotation..
                List<AnnotationTag> annotTagList =AllComponents.getFileTag().getAnnotation();
                //List<AnnotationTag> annotTagList = fileTag.getAnnotation();
                JLabel label = (JLabel) me.getComponent();
                String commentText = "";
                AnnotationTag annoTag = new AnnotationTag();
                if( annotTagList != null && !annotTagList.isEmpty() ) {
                    for( AnnotationTag annot : annotTagList ) {
                        if( annot.getKey().equals( label.getName() ) ) {
                            commentText = annot.getValue();
                            annoTag = annot;
                            break;
                        }
                    }
                }

                LOGGER.log( Location.NH,
                        "Comment icon clicked to view the annotation." );

                AllComponents.highlightAnnotInHistoryTable( annoTag.getKey(), commentText );
                // highlightAnnotation(commentText);

                // // COMMENTING THE CODE FOR THE ANNOTATION POP UP
                // CommentHandler commentHdlr = new
                // CommentHandler(commentText);
                // commentHdlr.setVisible(true);
                // try {
                // if (commentHdlr.getCommentText() == null
                // || StringUtils.isEmpty(commentHdlr
                // .getCommentText())) {
                // // remove the annotation as it is delete operation
                // highlight(document, label.getName(), Color.WHITE);
                // removeAnnotation(label.getName());
                // removeCommentsComponents(label.getName(), null);
                // } else {
                // updateAnnotation(annoTag.getX(), annoTag.getY(),
                // label.getName(), commentHdlr
                // .getCommentText(), xmlFileName);
                // xmlService.marshall(xmlFileName, fileTag);
                // xmlService.marshall(historyFilePath, hisFileTag);
                // }
                // } catch (CollabVizException e) {
                // System.out
                // .println("ERROR: Error while updating the annotation.");
                // }
            }
        } );
    }
}
